/*
 * tune.c
 *
 *  Created on: 2012-9-4
 *      Author: Administrator
 */
#include "tangoc.h"


void t_raster_menu()
{
	puts("\nLift menu, press a key in brackes.");
	puts("[1] Lift X sense Y no offset");
	puts("[2] Lift X sense Y with offset");
	puts("[3] Lift Y sense X no offset");
	puts("[4] Lift Y sense X with offset");
//	puts("[S] Show menu");
	puts("[B] Back raster menu");
	puts("[X] Exit");
}

//void show_t_raster_raw(int j, short int raw[], int len, char flag, int fptr)
//{
//	char buf[512];
//	char tmp[8];
//	int i;
//
//	j == 0 ? strcpy(buf, "s: ") : strcpy(buf, "r: ");
//	for (i = 0; i < len; i++)
//	{
//		if (raw[i] > 999) raw[i] = 999;
//		else if (raw[i] < -999) raw[i] = -999;
//		sprintf(tmp, "%4d ", raw[i]);
//		strcat(buf, tmp);
//	}
//	strcat(buf, "\n");
//	printf("%s", buf);
//
//	if (flag == 'Y' || flag == 'y') write(fptr, buf, strlen(buf));
//}

int t_raster_set(int type, int mode, char flag)
{
	int i, j, ret, fptr = -1;
	unsigned char xline = 0;
	unsigned char yline = 0;
	unsigned char buf[5120];
	short int xyraw[128];

	active_power();
	ioctl(gdev, DISABLE_IRQ, 0);
	for (i = 0; i < 3; i++)
	{
		getXYN(buf, 2);
		xline = buf[0];
		yline = buf[1];
		if (xline == 0 || yline == 0 || xline > 48 || yline > 48) continue;
		else break;
	}

	printf("xline=%d yline=%d\n", xline, yline);
	if (xline == 0 || yline == 0 || xline > 48 || yline > 48)
	{
		puts("read scan lines error.");
		ioctl(gdev, ENABLE_IRQ, 0);
		restore_power_mode();
		return 0;
	}
	xline++;
	yline++;

	if (flag == 'Y' || flag == 'y')
	{
		char fdat[32] = "";
		get_time(mode, fdat);
		fptr = open(fdat, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
		if (fptr == -1)
		{
			puts("Open raw.dat failed.");
			ioctl(gdev, ENABLE_IRQ, 0);
			restore_power_mode();
			return 0;
		}
	}

	set_mode(mode, 0);
	init_keyboard();
	while (1)
	{
		if (kbhit())
		{
			char key = getkey();
			if (key == '\n')
			{
				enable_algo();
				ioctl(gdev, RESET_TP, 0);
				msleep(100);
				ioctl(gdev, ENABLE_IRQ, 0);
				restore_power_mode();
				break;
			}
		}

		ret = read_raster(buf, xline, yline);

//      show buf data
//		{
//			for(j=0; j<yline; j++)
//			{
//				for (i = 0; i < xline * 2; i++)
//				{
//					printf("%d ", buf[j*xline*2 + i]);
//				}
//				printf("\n");
//			}
//		}

		for (j = 0; j < yline; j++)
		{
			for (i = 0; i < xline; i++)
			{
				xyraw[i] = (buf[(j * xline + i) * 2 + 1] << 8) | buf[(j * xline + i) * 2];
			}
			show_raster_raw(j, xyraw, xline, flag, fptr);
		}
	}
	close_keyboard();
	if (flag == 'Y' || flag == 'y')
	{
		close(fptr);
	}

	return 1;
}

void t_raster_mode()
{
	char cmd, flag = 'n';
	int type = 1;
	unsigned char loop = 1;

	t_raster_menu();
	while(loop)
	{
		printf("Input cmd: ");
		cmd = getchar();
		while (getchar() != '\n');
		switch(cmd)
		{
		case '1':
			input_raster_param(&flag);
			t_raster_set(type, RASTER_LX_SY, flag);
			break;
		case '2':
			input_raster_param(&flag);
			t_raster_set(type, RASTER_LX_SY_OFFSET, flag);
			break;
		case '3':
			input_raster_param(&flag);
			t_raster_set(type, RASTER_LY_SX, flag);
			break;
		case '4':
			input_raster_param(&flag);
			t_raster_set(type, RASTER_LY_SX_OFFSET, flag);
			break;
		case 'b':
		case 'B':
			loop = 0;
			main_menu();
			break;
		case 'x':
		case 'X':
			finish();
			break;
		}
		t_raster_menu();
	}
}

